package com.situ.mall.service.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.mapper.CartMapper;
import com.situ.mall.pojo.entity.Cart;
import com.situ.mall.pojo.entity.Order;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.pojo.vo.CartProductVO;
import com.situ.mall.service.ICartService;

@Service
public class CartServiceImpl implements ICartService{
	@Autowired
	private CartMapper cartMapper;

	@Override
	public boolean insert(Cart cart) {
		// productId  userId
		int count = cartMapper.isExist(cart);
		// int count = isExist ? cartMapper.updateQuantity(cart):cartMapper.insert(cart);
		if (count == 1) {// 原来已经在购物车中就更新一下数量
			count = cartMapper.updateQuantity(cart);
		} else {// 原来没有在购物车中就插入
			count= cartMapper.insert(cart);
		}
		return count == 1;
	}

	@Override
	public List<CartProductVO> selectByUserId(Integer id) {
		return cartMapper.selectByUserId(id);
	}

	@Override
	public boolean updateQuantity(Cart cart) {
		int count = cartMapper.updateQuantity(cart);
		return count == 1;
	}

	@Override
	public boolean deleteById(Integer cartId) {
		int count = cartMapper.deleteByPrimaryKey(cartId);
		return count == 1;
	}

	@Override
	public boolean updateStatus(Integer cartId, Integer checked) {
		int count = cartMapper.updateStatus(cartId, checked);
		return count == 1;
	}

	@Override
	public List<CartProductVO> selectByUserIdAndChecked(Integer id) {
		return cartMapper.selectByUserIdAndChecked(id);
	}

}
